﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class superUser : CWebBase
{
    public CDatabase _db = new CDatabase();
    public Hashtable json = new Hashtable();

    protected void Page_Load(object sender, EventArgs e)
    {
        // 8887 만 입력 가능.
        if (Userinfo.UserID != "8887")
        {
            JustGo("/analyze_new/admin/level/levelMain.aspx");
        }

        string action = CheckNull(Request["action"]);

        if (action == "save") {
            _db.GetConnection();

            string userid = RemoveSpecialChars(CheckNull(Request["_user_id"]));
            string userpw = RemoveSpecialChars(CheckNull(Request["_user_password"]));
            string usernm = CheckNull(Request["_user_name"]);
            string sql = "Select count(*) From analyze_admin where userid = '" + userid + "' and UseYn = 'Y'";

            // super user
            if(userid == "8887"){
                JustGo("/");
                return;
            }

            int cnt = _db.GetQueryInt(sql);

            userpw = EncodeMd5(userpw);
            // update
            if (cnt > 0)
            {
                sql = "Update analyze_admin set UserPassWord = '" + userpw + "' where UserId = '" + userid + "'";
            }

            // insert
            else {
                sql = "Insert into analyze_admin (UserId, UserPassWord, UserName, UseYn) values ('" + userid + "', '" + userpw + "', '" + usernm + "', 'Y')";
            }

            SqlDataReader dr = _db.GetQueryResult(sql);
            dr.Close();

            json.Add("message", "Y");
            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(json));
        }

        return;
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        _db.ReleaseConnection();
    }
}
